package com.example.basic.cmdprocess;

import java.io.IOException;

public class ProcessUtil {
	
	private static final String TAG =  "ProcessUtil";
	private static final int DEFAULT_EXECUTE_TIMEOUT = 30*1000;
	static final String FAILED = "Failed";
	
	public static Process getAdbshell(String localAdbPath,String sn) {
		Process process = null;
		String adbshell = localAdbPath + "-s" + sn + " shell";
		
		try {
			process = Runtime.getRuntime().exec(adbshell);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return process;
	}

	/**
	 * 执行cmd命令
	 */
	public static ExecuteResult executeCmd(String cmd) {
		return executeCmd(cmd,DEFAULT_EXECUTE_TIMEOUT,null,false);
	}
	
	public static ExecuteResult executeCmd(String[] cmds) {
		return executeCmd(cmds,DEFAULT_EXECUTE_TIMEOUT,new String[] {},false);
	}
	
	public static ExecuteResult executeCmd(String[] cmds , int timeoutMs) {
		return executeCmd(cmds,timeoutMs,new String[] {},false);
	}
	
	public static ExecuteResult executeCmd(String[] cmds , boolean Logprint) {
		return executeCmd(cmds,DEFAULT_EXECUTE_TIMEOUT,new String[] {},Logprint);
	}
	
	public static ExecuteResult executeCmd(String[] cmds , String breakString) {
		return executeCmd(cmds,DEFAULT_EXECUTE_TIMEOUT,new String[] {breakString},false);
	}
	
	public static ExecuteResult executeCmd(String cmd,int timeoutMs) {
		return executeCmd(cmd,timeoutMs,null,false);
	}
	
	public static ExecuteResult executeCmd(String cmd,boolean logPrint) {
		if(logPrint)
		System.out.println("executeCmd(String cmd,boolean logPrint)");
		return executeCmd(cmd,DEFAULT_EXECUTE_TIMEOUT,null,logPrint);
	}
	
	public static ExecuteResult executeCmd(String cmd,String breakinfo) {
		return executeCmd(cmd,DEFAULT_EXECUTE_TIMEOUT,breakinfo,false);
	}
	
	public static ExecuteResult executeCmd(String cmd,long timeout,String breakString,boolean logPrint) {
		if(cmd == null) {
			System.out.println("execute cmd is null...");
			return new ExecuteResult(false, null, null);
		}
		return executeCmd(new String[] { cmd }, timeout , new String[] {breakString} , logPrint);
	}
	
	public static ExecuteResult executeCmd(String cmd , long timeout , String[] breakString) {
		return executeCmd(new String[] { cmd }, timeout , breakString , false);
	}
	
	public static ExecuteResult executeCmd(String[] cmds , long timeout , String[] breakStrings , boolean logPrint) {
		if(cmds == null) {
			return new ExecuteResult(false, null,null);
		}
		boolean executeResult = true;
		CmdProcessExecutor executor = new CmdProcessExecutor(breakStrings, cmds, logPrint, timeout);
		executeResult = executor.execute();
		return new ExecuteResult(executeResult, executor.getResult());
	}
	
	public static ExecuteResult executeCmdByProcessBuilder(String...cmds) {
		return getExecuteCmdResult(cmds);
	}
	
	public static ExecuteResult getExecuteCmdResult(String[] cmds) {
		return getExecuteCmdResult(cmds,DEFAULT_EXECUTE_TIMEOUT);
	}
	
	public static ExecuteResult getExecuteCmdResult(String[] cmds,boolean logprint) {
		return getExecuteCmdResult(cmds,null,DEFAULT_EXECUTE_TIMEOUT,logprint);
	}
	
	public static ExecuteResult getExecuteCmdResult(String[] cmds,long timeout) {
		return getExecuteCmdResult(cmds,null,timeout);
	}
	
	public static ExecuteResult getExecuteCmdResult(String[] cmds,String[] breaks,long timeout) {
		return getExecuteCmdResult(cmds,breaks,timeout,false);
	}
	
	public static ExecuteResult getExecuteCmdResult(String[] cmds,String[] breaks,long timeout,boolean logprint) {
		if(cmds == null) {
			return new ExecuteResult(false, null,null);
		}
		ProcessBuilderExecutor executor = new ProcessBuilderExecutor(cmds, timeout, breaks);
		executor.execute();
		executor.cancleExector();
		return new ExecuteResult(true, executor.getResult());
	}
	
	private static String getCmdsInfo(String[] cmds) {
		StringBuffer cmdinfos = new StringBuffer();
		if(cmds != null) {
			for(String cmd:cmds) {
				cmdinfos.append(cmd);
				cmdinfos.append(";");
			}
		}
		return cmdinfos.toString();
	}
}
